package cn.kongwg.dao.impl;

import cn.kongwg.bean.Book;
import cn.kongwg.dao.BaseDao;
import cn.kongwg.dao.BookDao;

import java.util.List;

/**
 * @author kongwg
 * @create 2021-01-26 9:49
 */
public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public void addBook(Book book) {
        String sql = "insert into t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
        update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());
    }

    @Override
    public void deleteBook(Integer id) {
        String sql = "delete from t_book where id = ?";

        update(sql,id);
    }

    @Override
    public void updateBook(Book book) {
        String sql = "update t_book set `name`=?,`author`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where id = ?";

        update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` as imgPath from t_book where id= ?";
        return queryForOne(Book.class,sql,id);
    }

    @Override
    public List<Book> queryForAll() {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` as imgPath from t_book ";

        return queryForList(Book.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from t_book";
        Number count = (Number) queryForSingle(sql);
        return count.intValue();
    }

    @Override
    public List<Book> queryForPageItem(Integer begin,Integer pageSize) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` as imgPath from t_book  limit ?,?";
        return queryForList(Book.class,sql,begin,pageSize);

    }

    @Override
    public Integer queryForPageTotalCountByPrice(Integer min, Integer max) {
        String sql = "select count(*) from t_book where price between ? and ? ";
        Number count = (Number) queryForSingle(sql,min,max);
        return count.intValue();
    }

    @Override
    public List<Book> queryForPageItemsByPrice(Integer begin, Integer pageSize,Integer min,Integer max) {
        String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` as imgPath from t_book where price between ? and ? order by price limit ?,?";
        return queryForList(Book.class,sql,min,max,begin,pageSize);
    }
}
